package com.base.cn.platform.os.controller.count;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.manage.count.PageViewCountService;
import com.base.cn.platform.os.service.manage.order.count.OrderCountService;
import com.base.cn.platform.os.service.manage.user.count.UserCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;

/**
 * 访问统计Controller
 *
 * @author s.li
 * @create 2018-08-23-21:00
 */
@Controller
@RequestMapping("/admin/count")
public class AccessStatisticsController extends AdminBaseController {

    @Autowired
    private UserCountService userCountService;
    @Autowired
    private OrderCountService orderCountService;
    @Autowired
    private PageViewCountService pageViewCountService;

    /**
     * 初始化访问数据统计页面
     * @return
     */
    @RequestMapping("/accessCount")
    public String accessCount(){
        return resultPage("/count/access-statistics");
    }

    /**
     * 查询基本数据统计
     * @return
     */
    @RequestMapping("/getAccessCountNum")
    @ResponseBody
    public Map<String,Object> getAccessCountNum(@RequestBody Map<String,String> map){
        //获取PV、UV统计数据
        Map<String,Integer> pageUVpvSumNumMap = pageViewCountService.queryPageUVpvSumNum(map);
        //注册用户总数
        Map<String,Integer> userNumMap = userCountService.findUserCount();
        //活跃用户数量
        int activeUserNum = userCountService.queryActiveUserNum();
        //获取购课用户量
        int buyCourseUserNum = orderCountService.queryBuyCourseUserNum();
        Map<String,Object> resultMap = ResultUtil.SUCCESS();
        resultMap.put("pageUVpvSumNumMap",pageUVpvSumNumMap);
        resultMap.put("userNumMap",userNumMap);
        resultMap.put("activeUserNum",activeUserNum);
        resultMap.put("buyCourseUserNum",buyCourseUserNum);
        return resultMap;
    }

    /**
     * 今日网站访问量曲线
     * @return
     */
    @RequestMapping("/queryUserLoginDayTimeNum")
    @ResponseBody
    public Map<String,Object> queryUserLoginDayTimeNum(@RequestParam("date") String date){
        if(StringUtils.isEmpty(date)){
            date =   DateUtils.format(new Date(),"yyyy-MM-dd");
        }
        List<Map<String,Integer>> userLoginDayTimeNumList = userCountService.queryUserLoginDayTimeNum(date);
        Map<String,Object> resultMap = ResultUtil.SUCCESS(userLoginDayTimeNumList);
        int max = 0;
        if(ObjectUtils.isNotEmpty(userLoginDayTimeNumList)){
            IntSummaryStatistics regNum = userLoginDayTimeNumList.stream().mapToInt((x) -> x.get("regNum")).summaryStatistics();
            IntSummaryStatistics nRegNum = userLoginDayTimeNumList.stream().mapToInt((x) -> x.get("nregNum")).summaryStatistics();
            IntSummaryStatistics buyNum = userLoginDayTimeNumList.stream().mapToInt((x) -> x.get("buyNum")).summaryStatistics();
            max = regNum.getMax();
            max = Math.max(max,nRegNum.getMax());
            max = Math.max(max,buyNum.getMax());
        }
        resultMap.put("max",max);
        return resultMap;
    }

    /**
     * 注册对比柱型图
     * @return
     */
    @RequestMapping("/queryRegFromUserNum")
    @ResponseBody
    public Map<String,Object> queryRegFromUserNum(){
        Map<String,Integer> regFromUserNumMap = userCountService.queryRegFromUserNum();
        return ResultUtil.SUCCESS(regFromUserNumMap);
    }

    /**
     * 平台栏目-频道-访问占比
     * @return Map<String,Object>
     */
    @RequestMapping("/queryPageViewCountNum")
    @ResponseBody
    public Map<String,Object> queryPageViewCountNum(){
        List<Map<String,Integer>> pageViewCountNumList = pageViewCountService.queryPageViewCountNum();
        return ResultUtil.SUCCESS(pageViewCountNumList);
    }

    /**
     * 网站某段时间访问量曲线
     * @return
     */
    @RequestMapping("/queryUserLoginTimeNum")
    @ResponseBody
    public Map<String,Object> queryUserLoginTimeNum(@RequestBody Map<String,Object> map){
        List<Map<String, Object>> maps = userCountService.queryUserLoginTimeNum(map);
        Map<String,Object> resultMap = ResultUtil.SUCCESS(maps);
        int max = 0;
        if(ObjectUtils.isNotEmpty(maps)){
            IntSummaryStatistics regNum = maps.stream().mapToInt((x) -> Integer.valueOf(x.get("regNum").toString())).summaryStatistics();
            IntSummaryStatistics nRegNum = maps.stream().mapToInt((x) ->Integer.valueOf(x.get("nRegNum").toString())).summaryStatistics();
            IntSummaryStatistics buyNum = maps.stream().mapToInt((x) -> Integer.valueOf(x.get("buyNum").toString())).summaryStatistics();
            max = regNum.getMax();
            max = Math.max(max,nRegNum.getMax());
            max = Math.max(max,buyNum.getMax());
        }
        resultMap.put("max",max);
        return resultMap;
    }
}
